package com.xmd.red.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * Created by superbone on 2016/11/2.
 */
public class AESUtil {

    /** 密钥算法 */
    private static final String KEY_ALGORITHM = "AES";
    private static final int KEY_SIZE = 128;
    private static final String password = "VxDksHQiTvQt9MMPtMVXdA==";


    public static String encrypt(String data,String password) throws Exception{

        byte[] dataBytes = data.getBytes("UTF-8");
        byte[] passwordBytes = Base64.decodeBase64(password);


         //还原密钥
        Key k = new SecretKeySpec(passwordBytes,KEY_ALGORITHM);

        Cipher cipher = Cipher.getInstance("AES");
        //初始化，设置为加密模式
        cipher.init(Cipher.ENCRYPT_MODE,k);
        //执行操作
        byte[] resultBytes = cipher.doFinal(dataBytes);
        return Base64.encodeBase64String(resultBytes);
    }

    public static void main11(String args[]) throws Exception{

        String result = encrypt("123456",password);
        System.out.println(result);
    }
}
